package com.briup.ptp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.ptp.mapper.slaughterMapper;
import com.briup.ptp.entity.Slaughter;
import com.briup.ptp.service.SlaughterService;
import com.briup.ptp.utils.PTPException;
import com.briup.ptp.utils.ResultCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Classname SaughterServiceImpl
 * @Description TODO
 * @Date 2024/2/23 16:34
 * @Created by 孙雅如
 * @Version v1.0.0
 */
@Service
public class SlaughterServiceImpl  extends ServiceImpl<slaughterMapper, Slaughter> implements SlaughterService {
    @Autowired
    private slaughterMapper mapper;


    @Override
    public PageInfo<Slaughter> SlaughterQueryPage( String name, String pigSpecies,Integer pageNum, Integer pageSize) {
        //开启PageHelper分页插件
        PageHelper.startPage(pageNum, pageSize);
//调用Mapper层代码
        List<Slaughter> list = mapper.slaughterQueryPage(name, pigSpecies);
//封装为PageInfo对象
        PageInfo pageInfo = new PageInfo<>(list);
//返回统一响应

        return pageInfo;
    }

    @Override
    //根据具体的数据情况 来判断插入还是修改
    public void saveSlaughter(Slaughter slaughter) {
        if(slaughter.getId() == null){
            mapper.saveSlaughter(slaughter);
        }else {
            mapper.updateSlaughter(slaughter);
        }
    }

    @Override
    public void updateSlaughter(Integer id) {
        //查询状态指定id对应的
        Slaughter slaughter=mapper.findByIdSlaughter(id);
        if(slaughter == null){
            throw  new PTPException(ResultCode.DATA_NONE);
        }
        //修改对象数据
        mapper.updateSlaughter(slaughter);
    }


}
